/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package common

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"saas_sys_base/redishelper"
	//"github.com/mjiulee/lego/utils"
)

const REDIS_Common_Area_List string = "redis_common_area_list"




func (t *Area) GetAreas() []*Area {
	var areaList []*Area = []*Area{}
	err := redishelper.RedisGetKeyEx(REDIS_Common_Area_List, areaList)
	if err == nil {
		return areaList
	}

	session := lego.GetDBEngine().Table("tb_area").Alias("t1")

	err = session.Find(&areaList)

	if err != nil {
		lego.LogError(err)
		return areaList
	}
	_ = redishelper.RedisSetKeyEx(REDIS_Common_Area_List, areaList)
	return areaList
}

/* 根据id获取记录
 * -----------------
 */
func (t *Area) RecodeById(id int64) *Area {
	item := new(Area)
	_, err := lego.GetDBEngine().ID(id).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *Area) AddRecode(item2add *Area) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *Area) AddRecodeWithsession(session *xorm.Session, item2add *Area) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *Area) DelRecodeById(id int64) bool {
	item := new(Area)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *Area) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(Area)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *Area) UpdateRecode(rc *Area) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *Area) UpdateRecodeWithSession(session *xorm.Session, rc *Area) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}
